<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in Employess"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />

        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="$emit('cancle')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addDepartments, changeDepartments, updateDepartments } from '@/api/departments'
import { getEmployess } from '@/api/employess'
export default {
  name: 'VueHr3Dialog',
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    orginList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 自定义效验部门编码的唯一性
    const validCode = (rule, value, callback) => {
      // console.log(value)
      // console.log(this.orginList)
      // 通过传递过来的数据找出所有的code
      let newCode = this.orginList.map(item => item.code)
      // 点击编辑删除自身的code
      if (!this.isEdit) {
        newCode = this.orginList.filter(item => item.id !== this.id).map(item => item.code)
        console.log(newCode)
      }
      if (newCode.includes(value)) {
        callback(new Error(`部门编码 ${value} 已存在`))
      } else {
        callback()
      }
    }
    // 自定义效验部门名称  同级中不能有重复的
    const validName = (rule, value, callback) => {
      // console.log(this.id)
      let newName = this.orginList.filter(item => item.pid === this.id).map(item => item.name)
      if (!this.isEdit) {
        // 找到点击的的部门的对象  再找出pid
        const curName = this.orginList.find(item => item.id === this.id)
        const pid = curName.pid
        // console.log(curName)
        // console.log(pid)
        // 找到所有的兄弟部门
        const borter = this.orginList.filter(item => item.pid === pid)
        newName = borter.filter(item => item.id !== this.id).map(item => item.name)
      }
      // console.log(newName)

      if (newName.includes(value)) {
        callback(new Error(`部门名称 ${value} 已存在`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      Employess: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadDepartments()
    if (!this.isEdit) {
      this.getUpdateDepartmentd()
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    // 获取当前部门信息
    async getUpdateDepartmentd() {
      try {
        const res = await updateDepartments(this.id)
        // console.log(res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑部门
    async doEdit() {
      try {
        const res = await changeDepartments(this.form)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.$emit('success')
      } catch (error) {
        console.log('编辑失败')
      }
    },
    hSubmit() {
      // 兜底效验
      this.$refs.deptForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doAdd() : this.doEdit()
      })
    },
    // 新增部门
    async doAdd() {
      try {
        // 通过展开运算符进行浅拷贝 确定添加部门的父级id
        const res = await addDepartments({ ...this.form, pid: this.id })
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 子传父
        this.$emit('success')
      } catch (error) {
        console.log('添加失败')
      }
    },
    // 获取信息
    async  loadDepartments() {
      try {
        const res = await getEmployess()
        // console.log(res)
        this.Employess = res.data
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
